///////////////////////////////////////////////////////////////
// File: "CMainMenuState.h"
// Author: James Gonzalez (JG)
// Purpose: To contain all Main Menu functionality (play, options, credits, how to, exit)
///////////////////////////////////////////////////////////////


#include "CMainMenuState.h"
#include "../Wrappers/CSGD_DirectInput.h"
//#include "CGamePlayState.h"
#include "CBitmapFontEngine.h"
#include "CGame.h"
#include "CWiimote.h"
#include "CAnimationManager.h"
#include "CSoundManager.h"
#include "CLevelSelectState.h"
#include "CProfileSelectState.h"
#include "CHighScoresState.h"
#include "CTransitionScreen.h"
#include "CGamePlayState.h"
#include "CTallyState.h"
#include "CAttractState.h"
#include "COptionsState.h"
#include "CSoundManager.h"
#include "CCreditsState.h"
#include "CHowToPlayState.h"


CMainMenuState::CMainMenuState()
{
	m_nChoice = 0;
	m_nFontID = -1;
	
	m_bExitToOptions = false;
	m_bExitToPlay= false;
	m_bExitToHowToPlay= false;
	m_bExitToCredits= false;
	m_bExitGame= false;

	///Transition members/////////////////////////////////////////////////////
	m_cTransitionScreen = CTransitionScreen::GetInstance();
	m_bExitingState = false;
	m_bStartup = true;
	//////////////////////////////////////////////////////////////////////////

}

CMainMenuState::~CMainMenuState()
{

}

void CMainMenuState::Enter()
{
	m_nFontID = CBitmapFontEngine::GetInstance()->LoadFont("BitmapFonts/BS_TechDemoTitle.xml");
	m_cScreen.LoadScreen("Screens/BS_MainMenuScreen.xml");
	
	m_bExitingState = false;
	m_bStartup = true;
	m_fAttract = 0.0f;
	m_nChoice = 0;

}

void CMainMenuState::Exit()
{
	m_cScreen.ExitScreen();	
	m_bExitToOptions = false;
	m_bExitToPlay= false;
	m_bExitToHowToPlay= false;
	m_bExitToCredits= false;
	m_bExitGame= false;

}

bool CMainMenuState::Input(float fElapsedTime)
{

	CWiimote* pWii = CWiimote::GetInstance();
	//wiiuse_poll(pWii->GetWiimote(), MAX_WII_REMOTES);
	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();

	if (pWii->IsButtonPressed(WII_HOME) || pDI->KeyPressed(DIK_ESCAPE))
	{
		CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_SELECT);
		return false;
	}
	
	if (pWii->IsButtonJustPressed(WII_A) || pDI->KeyPressed(DIK_RETURN))
	{
		CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_SELECT);

		switch(m_nChoice)
		{
		case 0:
			m_bExitToPlay = true;
			break;
		case 1:
			m_bExitToOptions = true;
			break;
		case 2:
			m_bExitToHowToPlay = true;
			break;
		case 3:
			m_bExitToCredits = true;
			break;
		case 4:
			m_bExitGame = true;
			break;
		}
	}
	if (pWii->IsButtonJustPressed(WII_UP) || pDI->KeyPressed(DIK_UP))
	{
		CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_MOVE);
		m_nChoice--;
		m_fAttract = 0.0f;
	}
	else if (pWii->IsButtonJustPressed(WII_DOWN) || pDI->KeyPressed(DIK_DOWN))
	{
		CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_MOVE);
		m_nChoice++;
		m_fAttract = 0.0f;
	}


	//DEBUG Testing High Score state
	if (pWii->IsButtonPressed(WII_ONE) || pDI->KeyPressed(DIK_H))
	{
		//Set this bool true only if the game is won
		CHighScoresState::GetInstance()->IsEndOfGame(false);
		m_cTransitionScreen->ChangeTo(CHighScoresState::GetInstance());
	}

	if (m_nChoice > m_cScreen.GetMenuCount()-1)
	{
		m_nChoice = 0;
	}
	else if (m_nChoice < 0)
	{
		m_nChoice = m_cScreen.GetMenuCount()-1;
	}



	//Test door///////////////////////////////////////////////////////////////
	if ( pDI->KeyPressed(DIK_1))
	{
		m_cTransitionScreen->OpenDoor();
	}
	if ( pDI->KeyPressed(DIK_2))
	{
		m_cTransitionScreen->CloseDoor();
	}
	//////////////////////////////////////////////////////////////////////////

	///Close door then change state///////////////////////////////////////////
	if (m_bExitToPlay)
	{
		m_cTransitionScreen->ChangeTo(CProfileSelectState::GetInstance());	
	}
	else if(m_bExitToOptions)
	{
		m_cTransitionScreen->ChangeTo(COptionsState::GetInstance());	
	}
	//TODO : Create other states
	else if(m_bExitToHowToPlay)
	{
		m_cTransitionScreen->ChangeTo(CHowToPlayState::GetInstance());	
	}
	else if (m_bExitToCredits)
	{
		m_cTransitionScreen->ChangeTo(CCreditsState::GetInstance());	
	}
	else if(m_bExitGame)
	{
		//m_cTransitionScreen->ChangeTo(CExitState::GetInstance());
		return false;
	}

	//////////////////////////////////////////////////////////////////////////

	return true;
}

void CMainMenuState::Update(float fElapsedTime)
{

	//////////////////////////////////////////////////////////////////////////
	///Open Door on entering state
	//m_fTransitionTimer+= fElapsedTime;
 	if (fElapsedTime < 1)
 	{
 		if (m_bStartup)
		{
			m_cTransitionScreen->OpenDoor();
			m_bStartup = false;
		}

		m_fAttract += fElapsedTime;

		if (m_fAttract > 15.0f)
		{
			m_cTransitionScreen->ChangeTo(CAttractState::GetInstance());
		}
 	}
	
	//////////////////////////////////////////////////////////////////////////

}

void CMainMenuState::Render()
{
	m_cScreen.RenderScreen();


	CBitmapFontEngine* pBFE = CBitmapFontEngine::GetInstance();

	int TitleX =m_cScreen.GetPosObjMembers("Title",XPOS);
	int TitleY = m_cScreen.GetPosObjMembers("Title",YPOS);

	int PlayX = m_cScreen.GetMenuMembers("Play",XPOS);
	int PlayY = m_cScreen.GetMenuMembers("Play",YPOS);

	int OptionsX = m_cScreen.GetMenuMembers("Options",XPOS);
	int OptionsY = m_cScreen.GetMenuMembers("Options",YPOS);

	int HowToPlayX = m_cScreen.GetMenuMembers("HowToPlay",XPOS);
	int HowToPlayY = m_cScreen.GetMenuMembers("HowToPlay",YPOS);

	int CreditsX = m_cScreen.GetMenuMembers("Credits",XPOS);
	int CreditsY = m_cScreen.GetMenuMembers("Credits",YPOS);

	int ExitX = m_cScreen.GetMenuMembers("Exit",XPOS);
	int ExitY = m_cScreen.GetMenuMembers("Exit",YPOS);



	


	pBFE->RenderString("Blob Run", m_nFontID, 0, D3DCOLOR_XRGB(0, 255, 0), TitleX, TitleY , 0);

	pBFE->RenderString("Play",			m_nFontID, 0,D3DCOLOR_XRGB(0, 255, 0), PlayX,PlayY,  0);
	pBFE->RenderString("Options",		m_nFontID, 0,D3DCOLOR_XRGB(0, 255, 0), OptionsX,OptionsY,  0);
	pBFE->RenderString("How To Play",	m_nFontID, 0,D3DCOLOR_XRGB(0, 255, 0), HowToPlayX,HowToPlayY,  0);
	pBFE->RenderString("Credits",		m_nFontID, 0,D3DCOLOR_XRGB(0, 255, 0), CreditsX,CreditsY,  0);
	pBFE->RenderString("Exit",			m_nFontID, 0,D3DCOLOR_XRGB(0, 255, 0), ExitX,ExitY,  0);


	switch(m_nChoice)
	{

	case 0:
		//CAnimationManager::GetInstance()->DrawAnimation("Walking",PlayX-50,PlayY);
		//CAnimationManager::GetInstance()->DrawAnimation("Walking",PlayX+150,PlayY);
		pBFE->RenderString("Play",m_nFontID, 0,D3DCOLOR_XRGB(255, 255, 0), PlayX,PlayY,  0);
		break;
	case 1:
		//CAnimationManager::GetInstance()->DrawAnimation("Walking",OptionsX-50,OptionsY);
		//CAnimationManager::GetInstance()->DrawAnimation("Walking",OptionsX+150,OptionsY);
		pBFE->RenderString("Options", m_nFontID, 0,D3DCOLOR_XRGB(255, 255, 0), OptionsX,OptionsY,  0);

		break;
	case 2:
		//CAnimationManager::GetInstance()->DrawAnimation("Walking",HowToPlayX-50,HowToPlayY);
		//CAnimationManager::GetInstance()->DrawAnimation("Walking",HowToPlayX+150,HowToPlayY);
		pBFE->RenderString("How To Play",m_nFontID, 0,D3DCOLOR_XRGB(255, 255, 0), HowToPlayX,HowToPlayY,  0);

		break;
	case 3:
		//CAnimationManager::GetInstance()->DrawAnimation("Walking",CreditsX-50,CreditsY);
		//CAnimationManager::GetInstance()->DrawAnimation("Walking",CreditsX+150,CreditsY);
		pBFE->RenderString("Credits",m_nFontID, 0,D3DCOLOR_XRGB(255, 255, 0), CreditsX,CreditsY,  0);

		break;
	case 4:
		//CAnimationManager::GetInstance()->DrawAnimation("Walking",ExitX-50,ExitY);
		//CAnimationManager::GetInstance()->DrawAnimation("Walking",ExitX+150,ExitY);
		pBFE->RenderString("Exit",	m_nFontID, 0,D3DCOLOR_XRGB(255, 255, 0), ExitX,ExitY,  0);

		break;

		
	}


	

}